// word-games.js - 单词游戏主页逻辑
const util = require('../../../utils/util.js');
const audioUtils = require('../../../utils/audioUtils.js');
const app = getApp();
Page({
  data: {
    totalGamesPlayed: 0,
    totalStars: 0,
    highScore: 0,
    matchingStats: { played: 0, bestScore: 0 },
    scrambleStats: { played: 0, bestScore: 0 },
    memoryStats: { played: 0, bestScore: 0 },
    chainStats: { played: 0, bestScore: 0 },
    typingStats: { played: 0, bestScore: 0 },
    puzzleStats: { played: 0, bestScore: 0 }
  },

  onLoad: function() {
    this.loadGameStats();
  },

  onShow: function() {
    // 每次显示页面时重新加载数据
    this.loadGameStats();
  },

  // 加载游戏统计数据
  loadGameStats: function() {
    const storageManager = app.getStorageManager();
    try {
      const gameStats = storageManager.getStorage(storageManager.STORAGE_KEYS.WORD_GAME_STATS) || {
        totalGamesPlayed: 0,
        totalStars: 0,
        highScore: 0,
        matching: { played: 0, bestScore: 0 },
        scramble: { played: 0, bestScore: 0 },
        memory: { played: 0, bestScore: 0 },
        chain: { played: 0, bestScore: 0 },
        typing: { played: 0, bestScore: 0 },
        puzzle: { played: 0, bestScore: 0 }
      };

      // 确保所有游戏统计对象都已正确初始化，防止undefined值
      const safeStats = {
        totalGamesPlayed: gameStats.totalGamesPlayed || 0,
        totalStars: gameStats.totalStars || 0,
        highScore: gameStats.highScore || 0,
        matchingStats: gameStats.matching || { played: 0, bestScore: 0 },
        scrambleStats: gameStats.scramble || { played: 0, bestScore: 0 },
        memoryStats: gameStats.memory || { played: 0, bestScore: 0 },
        chainStats: gameStats.chain || { played: 0, bestScore: 0 },
        typingStats: gameStats.typing || { played: 0, bestScore: 0 },
        puzzleStats: gameStats.puzzle || { played: 0, bestScore: 0 }
      };

      this.setData(safeStats);
    } catch (error) {
      console.error('加载游戏统计失败:', error);
    }
  },

  // 开始游戏
  startGame: function(e) {
    // 播放点击音效
    audioUtils.playClickSound();
    
    const gameType = e.currentTarget.dataset.game;
    const gameUrls = {
      matching: '/pages/challenge/word-games/matching-game/matching-game',
      scramble: '/pages/challenge/word-games/scramble-game/scramble-game',
      memory: '/pages/challenge/word-games/memory-game/memory-game',
      chain: '/pages/challenge/word-games/chain-game/chain-game',
      typing: '/pages/challenge/word-games/typing-game/typing-game',
      puzzle: '/pages/challenge/word-games/puzzle-game/puzzle-game'
    };

    if (gameUrls[gameType]) {
      wx.navigateTo({
        url: gameUrls[gameType]
      });
    }
  },

  // 分享
  onShareAppMessage: function() {
    return {
      title: '快来一起玩单词游戏吧！',
      path: '/pages/challenge/word-games/word-games',
      // imageUrl: '/images/challenge.png'
    };
  },
  
  // 分享到朋友圈
  onShareTimeline: function() {
    return {
      title: '快来一起玩单词游戏吧！',
      query: 'from=timeline',
      // imageUrl: '/images/challenge.png'
    };
  }
});
